<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 * @version    v1.0.0 Beta @ 2008-10-16
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * Url (根据参数生成URL地址)
 *
 * @package    helper
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_View_Helper_Url extends EGP_View_Helper_Abstract
{

    /**
     * 初始化方法
     *
     * @param  array  $params
     * @return string
     */
    public function init(array $params = array())
    {
        $request = EGP_Http_Request::getInstance();

        $scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http';

        $module = isset($params['module']) ? $params['module'] : $request->getParam('module');
        $controller = isset($params['controller']) ? $params['controller'] : $request->getParam('controller');
        $action = isset($params['action']) ? $params['action'] : $request->getParam('action');

        $host = $request->getHttpHost();
        $path = "/$module/$controller/$action";

        $frontController = EGP_Controller_Front::getInstance();
        if ($frontController->getModule($module) !== false) {
            $hosts = $frontController->getModule($module)->getHostList();
            if (isset($hosts[0])) {
                $host = $hosts[0];
                $path = "/$controller/$action";
            }
        }
        unset($params['module'], $params['controller'], $params['action']);

        $query = $request->getQuery();
        !is_array($query) && $query = array();
        $params = array_merge($query, $params);

        foreach ($params as $key => $value)
            $path .= "/$key/$value";

        return $request->getParam('module') === $module ? $path : "$scheme://$host$path";
    }

}